package com.aihelp.androidtestdemo.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aihelp.androidtestdemo.R;

public class XMLDemo extends AppCompatActivity implements View.OnClickListener {
    Button btn_xml, btn_java;
    int resheight;
    int toolbarHeight;
    Context context;
    Toolbar toolbar;
    LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = XMLDemo.this;
        setContentView(R.layout.activity_xmldemo);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        resheight = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        resheight = context.getResources().getDimensionPixelSize(resheight);
        toolbarHeight = toolbar.getMeasuredHeight();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "resheight=" + resheight + "\t toolbarHeight=" + toolbarHeight, Snackbar.LENGTH_SHORT)
                        .setAction("Action", null).show();
            }
        });
        linearLayout = findViewById(R.id.view);
        btn_java = findViewById(R.id.btn_addjava_layout);
        btn_xml = findViewById(R.id.btn_addxml_layout);
        btn_xml.setOnClickListener(this);
        btn_java.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.btn_addxml_layout:
                //xml 添加布局
                toolbar.addView(addView1());

                break;
            case R.id.btn_addjava_layout:
                //Java代码添加布局
//                linearLayout.addView(addView2());
                linearLayout.addView(addView3());
                break;
        }
    }


    private View addView1() {
        View view = LayoutInflater.from(context).inflate(R.layout.item_toolbar, null);

        return view;
    }

    private View addView2() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        TextView tv = new TextView(this);
        ViewGroup.LayoutParams layoutParams1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tv.setGravity(Gravity.CENTER);
        tv.setLayoutParams(layoutParams1);
        tv.setText("动态代码添加布局");
        linearLayout.addView(tv);
        return linearLayout;
    }

    private View addView3() {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int) dp2px(context, 100));
        RelativeLayout rl = new RelativeLayout(this);

        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.ic_launcher);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(dp2px(context, 40), dp2px(context, 40));
        imageView.setLayoutParams(layoutParams);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
        rl.addView(imageView);


        return rl;
    }

    private int dp2px(Context context, int dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);

    }


}
